/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rbh.risk.converter;

import com.rbh.risk.entity.MainEvent;
import com.rbh.risk.service.MainEventService;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

/**
 *
 * @author M
 */
@ManagedBean(name = "mainEventConvert")
@RequestScoped
@FacesConverter(forClass = MainEvent.class)
public class MainEventConverter implements Converter {

    @ManagedProperty("#{mainEventService}")
    private MainEventService mainEventService;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        MainEvent result = null;
        try {
            result = mainEventService.getById(Integer.parseInt(value));
        } catch (Exception ex) {
        }
        return result;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        String result = "";
        try {
            MainEvent eventPrimary = (MainEvent) value;
            result = eventPrimary.getMainEventId()+"";
        } catch (Exception ex) {
        }
        return result;
    }

    public MainEventService getMainEventService() {
        return mainEventService;
    }

    public void setMainEventService(MainEventService mainEventService) {
        this.mainEventService = mainEventService;
    }
    
}